/**
 * 
 */
package de.nak.roommgmt.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.LookupDispatchActionSupport;

import de.nak.roommgmt.model.Lecture;
import de.nak.roommgmt.service.CourseService;
import de.nak.roommgmt.service.RoomService;

/**
 * Action class that handles the user actions in the lecture list page.
 * @author Stephan Anft
 */
public class EditLecturesAction extends LookupDispatchActionSupport {

	/**
	 * Starts the "Add lecture" operation.
	 * @param mapping The action mapping.
	 * @param form The action form.
	 * @param request The request.
	 * @param response The response.
	 * @return the forward.
	 * @throws Exception if an exception has occured.
	 */
	public ActionForward addLecture(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		LectureForm lectureForm = (LectureForm)form;
		// Create new empty lecture in action form
		Lecture lecture = new Lecture();
		lectureForm.setLecture(lecture);
		// Load the room list
		RoomService roomService = (RoomService)getWebApplicationContext().getBean("roomService");
		lectureForm.setAvailableRooms(roomService.listRooms());
		// Load the course list
		CourseService courseService = (CourseService)getWebApplicationContext().getBean("courseService");
		lectureForm.setAvailableCourses(courseService.listCourses());
		// Show the add page
		return mapping.findForward("add");
	}

	/** {@inheritDoc} */
	@Override
	protected Map<String, String> getKeyMethodMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("button.add", "addLecture");
		return map;
	}

}
